package com.yy.cloud.controller;

import com.yy.cloud.base.Result;
import com.yy.cloud.entity.CartItem;
import com.yy.cloud.entity.GoodsInfo;
import com.yy.cloud.openfeign.GoodsServiceFeign;
import com.yy.cloud.openfeign.ShopcartServiceFeign;
import com.yy.cloud.param.ComplexModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @date 2024/5/6
 */
@RestController
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private GoodsServiceFeign goodsServiceFeign;

    @Autowired
    private ShopcartServiceFeign shopcartServiceFeign;

    // 商品服务调用地址
    public static final String GOODS_SERVICE_URL = "http://goods-service";
    // 购物车服务调用地址
    public static final String SHOPCART_SERVICE_URL = "http://shopcart-service";

    @GetMapping("/saveOrder")
    public String orderDetail(@RequestParam("goodsId") int goodsId, @RequestParam("cartId") int cartId) {
        // 模拟调用商品服务
//        String goodsResult = restTemplate.getForObject(GOODS_SERVICE_URL + "/goods/" + goodsId, String.class);
        String goodsResult = goodsServiceFeign.goodsDetail(goodsId);
        // 模拟调用购物车服务
//        String shopCartResult = restTemplate.getForObject(SHOPCART_SERVICE_URL + "/shop-cart/" + cartId, String.class);
        String shopCartResult = shopcartServiceFeign.shopCartDetail(cartId);
        return "success ! goodsResult = " + goodsResult + ", shopCartResult" + shopCartResult;
    }

    @GetMapping("/order/simpleParamsTest")
    public String simpleParamsTest(@RequestParam("sellStatus") int sellStatus, @RequestParam("goodsId") int goodsId) {
        return goodsServiceFeign.goodsDetailByParams(sellStatus, goodsId); // Feign接口调用远程服务接口
    }

    @GetMapping("/order/listByIdArray")
    public String[] listByIdArray() {
        Integer[] goodsIds = new Integer[4];
        goodsIds[0] = 1;
        goodsIds[1] = 3;
        goodsIds[2] = 5;
        goodsIds[3] = 7;
        return goodsServiceFeign.listByIdArray(goodsIds);
    }

    @GetMapping("/order/listByIdList")
    public List<String> listByIdList() {
        List<Integer> goodsIds = new ArrayList<Integer>();
        for (int i = 0; i < 4; i++) {
            goodsIds.add(i);
        }
        return goodsServiceFeign.listByIdList(goodsIds);
    }

    /**
     * 修改商品库存信息
     *
     * @return 修改之后的商品信息
     */
    @GetMapping("/updateGoodsInfo")
    public GoodsInfo updateGoodsInfo() {
        GoodsInfo goodsInfo = new GoodsInfo(10000, "iphone 12", 1000);
        return goodsServiceFeign.updateGoodsInfo(goodsInfo);
    }


    /**
     * 模拟通过Feign接口对复杂类型对象进行处理
     *
     * @return 处理过的复杂对象
     */
    @GetMapping("/testComplexModel")
    public Result<ComplexModel> testComplexModel() {
        ComplexModel complexModel = new ComplexModel();
        complexModel.setRequestNum(13);

        complexModel.setCartItem(new CartItem(2024, "yaoyuan's shop cart"));

        GoodsInfo goodsInfo1 = new GoodsInfo(1, "iphone 12", 1000);
        GoodsInfo goodsInfo2 = new GoodsInfo(2, "G-SHOCK手表", 520);
        GoodsInfo goodsInfo3 = new GoodsInfo(3, "NICK", 2999);
        List<GoodsInfo> goodsInfos = new ArrayList<GoodsInfo>(Arrays.asList(goodsInfo1, goodsInfo2, goodsInfo3));

        complexModel.setGoodsInfos(goodsInfos);
        List<Integer> cartIds = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4));
        complexModel.setCartIds(cartIds);

        ComplexModel complexModelResult = goodsServiceFeign.testComplexModel(complexModel);
        return Result.success(200, "Feign接口模拟处理复杂类型对象成功", complexModelResult);
    }
}
